import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pocketfile/module/root/controller/bottom_nav_controller.dart';
import 'package:pocketfile/module/root/model/menu_code.dart';
import 'package:pocketfile/module/root/model/menu_item.dart';
import 'package:pocketfile/values/app_colors.dart';


// ignore: must_be_immutable
class BottomNavBar extends StatelessWidget {
  final Function(MenuCode menuCode) onNewMenuSelected;

  BottomNavBar({Key? key, required this.onNewMenuSelected}) : super(key: key);

  final navController = BottomNavController();

  final Key bottomNavKey = GlobalKey();

  @override
  Widget build(BuildContext context) {

    Color selectedItemColor = Colors.black87;
    Color unselectedItemColor = Colors.grey;
    List<BottomNavItem> navItems = _getNavItems();
    print('qqqqq');
    return Obx(
      () => BottomNavigationBar(
        key: bottomNavKey,
        items: navItems
            .map(
              (BottomNavItem navItem) => BottomNavigationBarItem(
                  icon: Icon(navItem.icon.icon,
                  color:
                      navItems.indexOf(navItem) == navController.selectedIndex
                      ? selectedItemColor
                      : unselectedItemColor,
                  ),

                  label: navItem.navTitle,
                  tooltip: ""),
            )
            .toList(),
        showSelectedLabels: true,
        showUnselectedLabels: true,
        type: BottomNavigationBarType.fixed,
        backgroundColor: AppColors.colorWhite,
        selectedItemColor: selectedItemColor,
        unselectedItemColor: unselectedItemColor,
        currentIndex: navController.selectedIndex,
        onTap: (index) {
          navController.updateSelectedIndex(index);
          onNewMenuSelected(navItems[index].menuCode);
        },
      ),
    );
  }

  List<BottomNavItem> _getNavItems() {
    return [
      const BottomNavItem(
        navTitle: "首页",
        icon: Icon(Icons.home),
        menuCode: MenuCode.HOME,
      ),
      const BottomNavItem(
          navTitle: "我的",
          icon: Icon(Icons.settings),
          menuCode: MenuCode.MINE)
    ];
  }
}
